/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package player.model;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import player.model.exceptions.NoNextTrackException;
import player.model.exceptions.NoPreviousTrackException;

/**
 * Plejlista
 * @author Andrej
 */
public class Playlist implements Serializable
{

    private List<Track> list = new LinkedList<Track>();
    private PlaylistModel plModel;
    private int currentTrackIndex = -1;
    private String name;
    private boolean loop = false;

    /**
     * Default konstruktor
     */
    public Playlist()
    {
    }

    /**
     * Konstruktor sa parametrom
     * @param list lista trackova
     */
    public Playlist(List<Track> list)
    {
        this.list = list;
    }

    /**
     * Copy konstruktor
     * @param playlist playlista koja se kopira
     */
    public Playlist(Playlist playlist)
    {
        this.list = playlist.list;
    }

    /**
     * Geter matrice objekata
     * @return matrica objekata koji cine playlistu
     */
    public Object[][] getPlaylistAsMat()
    {
        Object rec[] = this.list.toArray();
        Object mat[][] = new Object[rec.length][];

        for (int i = 0; i < rec.length; i++)
        {
            mat[i] = ((Track) rec[i]).toArray();
        }

        return mat;
    }

    /**
     * Geter duzine liste
     * @return ukupna duzina liste u sekundama
     */
    public int getLenght()
    {
        int lenght = 0;

        for (Track track : list)
        {
            lenght += track.getLenght();
        }

        return lenght;
    }

    /**
     * Dodaje track na listu
     * @param track track koji se dodaje
     */
    public void addTrack(Track track)
    {
        list.add(track);
    }

    /**
     * Ubauje track na listu
     * @param track track koji se ubacuje
     * @param position pozicija u listi
     */
    public void insertTrack(Track track, int position)
    {
        list.add(position, track);
    }

    /**
     * Uklanja track sa liste
     * @param track track koji se uklanja
     */
    public void removeTrack(Track track)
    {
        removeTrack(list.indexOf(track));
    }

    /**
     * Uklanja track sa liste
     * @param position pozicija tracka koji se uklanja
     */
    public void removeTrack(int position)
    {
        list.remove(position);
        if (currentTrackIndex >= position)
        {
            currentTrackIndex--;
        }
    }

    /**
     * Vraca sledeci track sa liste
     * @return sledeci track
     * @throws NoNextTrackException baca se u slucaju da ne postoji sledeci track
     */
    public Track getNext() throws NoNextTrackException
    {
        if (list.size() > currentTrackIndex + 1)
        {
            currentTrackIndex++;
            return list.get(currentTrackIndex);
        } else if (loop)
        {
            currentTrackIndex = -1;
            return getNext();
        } else
        {
            throw new NoNextTrackException("End of playlist reached.");
        }
    }

    /**
     * Vraca prethodni track
     * @return prethodni track sa liste
     * @throws NoPreviousTrackException baca se u slucaju da ne postoji prethodni track
     */
    public Track getPrevious() throws NoPreviousTrackException
    {
        if (currentTrackIndex - 1 >= 0)
        {
            currentTrackIndex--;
            return list.get(currentTrackIndex);
        } else
        {
            throw new NoPreviousTrackException("End of playlist reached.");
        }
    }

    /**
     * Postavlja trenutni track
     * @param track track koji se postavlja kao trenutni
     */
    public void setCurrentTrack(Track track)
    {
        currentTrackIndex = list.indexOf(track);
    }

    /**
     * Geter loopa
     * @return vraca true ako se playlista ponavlja
     */
    public boolean isLoop()
    {
        return loop;
    }

    /**
     * Seter loopa
     * @param loop true ako se playlista ponavlja
     */
    public void setLoop(boolean loop)
    {
        this.loop = loop;
    }

    /**
     * Geter modela
     * @return model tabele za playlistu
     */
    public PlaylistModel getPlModel()
    {
        plModel = new PlaylistModel(getPlaylistAsMat(), new Object[]
                {
                    "Artist", "Track Name", "Length"
                }, list.toArray());
        return plModel;
    }

    @Override
    public String toString()
    {
        return "Playlist{" + "list=" + list + '}';
    }
}
